package game.launch.frame;

import game.image.container.SimpleImageContainer;
import game.image.panel.HostPanel;
import game.image.panel.ImageTableModel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class RunGUI implements Runnable {

	private String title;
	private Dimension dimension;
	private JPanel panel;

	public RunGUI(String title, Dimension dimension) {
		super();
		this.title = title == null ? "" : title;
		this.dimension = dimension == null ? new Dimension() : dimension;
		this.panel = panel == null ? new JPanel() : panel;
	}

	public void run() {
		JFrame frame = new JFrame(title);
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		List<SimpleImageContainer> imageList = new ArrayList<SimpleImageContainer>();

		try {

			// CodeSource src =
			// RunGUI.class.getProtectionDomain().getCodeSource();
			// List<String> list = new ArrayList<String>();
			//
			// if (src != null) {
			// URL jar = src.getLocation();
			// ZipInputStream zip = new ZipInputStream(jar.openStream());
			// ZipEntry ze = null;
			//
			// while ((ze = zip.getNextEntry()) != null) {
			// String entryName = ze.getName();
			// if (entryName.endsWith(".jpg")) {
			// list.add(entryName);
			// }
			// }
			//
			// }
			// String[] array = list.toArray(new String[list.size()]);

			int index = 1;
			while (true) {
				String resourceString = Integer.toString(index) + ".jpg";
				URL resource = getClass().getResource(resourceString);
				URL imageURL = new URL(resource, resourceString);

				SimpleImageContainer simpleImageContainer = new SimpleImageContainer(
						imageURL);

				imageList.add(simpleImageContainer);
				index++;
			}

		} catch (IOException e) {

		}

		ImageTableModel imageTableModel = new ImageTableModel(imageList);
		HostPanel hostPanel = new HostPanel(imageTableModel);

		frame.add(hostPanel, BorderLayout.CENTER);
		frame.pack();
		frame.setSize(dimension);
		frame.setVisible(true);
	}
}
